package com.b2b.util;


import com.b2b.model.CompanyApiAuthorityDto;
import com.b2b.service.CompanyApiAuthorityService;
import org.springframework.stereotype.Component;
import java.util.List;

@Component
public class CompanyApiAuthorityUtil {
    private final CompanyApiAuthorityService companyApiAuthorityService;

    public CompanyApiAuthorityUtil(CompanyApiAuthorityService companyApiAuthorityService){
        this.companyApiAuthorityService = companyApiAuthorityService;
    }

    public Integer insertAPI(CompanyApiAuthorityDto companyApiAuthority){
        CompanyApiAuthorityDto companyApiAuthorityDto = new CompanyApiAuthorityDto();
        companyApiAuthorityDto.setApiName(companyApiAuthority.getApiName());
        int count = companyApiAuthorityService.selectCount(companyApiAuthorityDto);
        if (count>=0){
            count = -2;
            return count;
        }else {
            count = companyApiAuthorityService.insert(companyApiAuthority);
            return count;
        }
    }

    public List<CompanyApiAuthorityDto> getAll(){

        return companyApiAuthorityService.selectAll();
    }

    public CompanyApiAuthorityDto getById(Integer apiId){
        return companyApiAuthorityService.selectByPrimaryKey(apiId);
    }

    public Integer update(CompanyApiAuthorityDto companyApiAuthorityDto){
        int count = companyApiAuthorityService.selectCount(companyApiAuthorityDto);
        if (count<=0){
            count = -2;
        }else {
            count = companyApiAuthorityService.updateByPrimaryKeySelective(companyApiAuthorityDto);
        }
        return count;
    }


    public Integer delete(Integer apiId){
        return companyApiAuthorityService.deleteByPrimaryKey(apiId);
    }
}
